package org.aperture.eve.data;

import java.net.MalformedURLException;
import java.net.URL;
import java.io.*;
import java.security.cert.Certificate;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLPeerUnverifiedException;

import java.nio.file.*;

public class Get_My_Skills {

	private static String KEY_ID = "1254671";
	private static String V_CODE = "mY2DCtEF2geGaDpMmgJWnDpD4S7XJ0t5CUujehiSwanXid0BlN6lPmTogXTVTJT4";
	private static String CHARACTER_ID = "1037924120";
	
	public static void main(String[] args) throws Exception {
		new Get_My_Skills().getSkills();
	}
	
	private void getSkills() {
		try {
			URL skillTree = new URL("https://api.eveonline.com/char/CharacterSheet.xml.aspx?keyID=" + KEY_ID + "&vCode=" + V_CODE + "&characterID=" + CHARACTER_ID);
			System.out.println("https://api.eveonline.com/char/CharacterSheet.xml.aspx?keyID=" + KEY_ID + "&vCode=" + V_CODE + "&characterID=" + CHARACTER_ID);
//			URL skillTree = new URL("https://api.eveonline.com/char/CharacterSheet.xml.aspx?keyID=" + KEY_ID + "&vCode=" + V_CODE);
//			System.out.println("https://api.eveonline.com/char/CharacterSheet.xml.aspx?keyID=" + KEY_ID + "&vCode=" + V_CODE);
			HttpsURLConnection secureConnection = (HttpsURLConnection)skillTree.openConnection();
			print_https_cert(secureConnection);
			writeSkillTreeToFile(secureConnection.getInputStream());
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
		
	private void writeSkillTreeToFile(InputStream input) {
		InputStream inputStream = null;
		OutputStreamWriter outputStream = null;
		Path outputFile = Paths.get("D:/javatest/Sabrah_Skills.xml");
	
		try {
			DataInputStream dataInputStream = new DataInputStream(input);

			Files.createFile(outputFile);
			
			byte[] buffer = new byte[1024];
			try {

				int numRead;
				do {
//					System.out.println("here.1.");
					numRead = dataInputStream.read(buffer);
					if (numRead > 0) {
//						System.out.print(buffer);
						Files.write(outputFile, buffer, StandardOpenOption.APPEND);
					}
				} while (numRead != -1);
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				if (inputStream != null) {
					try {
						inputStream.close();	
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				if (outputStream != null) {
					try {
						outputStream.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		} catch (IOException e) {
	    	  e.printStackTrace();
	      }
		  finally {
			if (inputStream != null) {
				try {
					inputStream.close();	
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (outputStream != null) {
				try {
				outputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	
	private void print_https_cert(HttpsURLConnection con){
		   
		if(con!=null){
			 
			try {
			 
				System.out.println("Response Code : " + con.getResponseCode());
				System.out.println("Cipher Suite : " + con.getCipherSuite());
				System.out.println("\n");
			 
				Certificate[] certs = con.getServerCertificates();
				for(Certificate cert : certs){
				   System.out.println("Cert Type : " + cert.getType());
				   System.out.println("Cert Hash Code : " + cert.hashCode());
				   System.out.println("Cert Public Key Algorithm : " + cert.getPublicKey().getAlgorithm());
				   System.out.println("Cert Public Key Format : " + cert.getPublicKey().getFormat());
				   System.out.println("\n");
				}
			} catch (SSLPeerUnverifiedException e) {
				e.printStackTrace();
			} catch (IOException e){
				e.printStackTrace();
			}
			 
	     }
	}

	
}
